package indi.zqc.mqtt.producer.send;

import com.alibaba.fastjson.JSONObject;
import indi.zqc.mqtt.producer.handler.MqttMessageGateway;
import indi.zqc.mqtt.producer.pojo.ApiData;
import indi.zqc.mqtt.producer.pojo.MqttObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static indi.zqc.mqtt.producer.Application.encodeBase64;

/**
 * TODO
 *
 * @author AZHE_SUN
 * @date 2021-6-26 17:44
 **/
@Component("ecg")
public class Producer implements Runnable  {

    @Autowired
    MqttMessageGateway gateway;

    @Override
    public void run() {
        MqttObject mqttObject = new MqttObject();
        List<String> list = new LinkedList<>();
        mqttObject.setId("11");
        list.add("222");
        list.add("333");
        mqttObject.setIds(list);
        try {
            ByteArrayOutputStream byteArrayOutputStream= new ByteArrayOutputStream();
            ObjectOutputStream o = new ObjectOutputStream(byteArrayOutputStream);
            o.writeObject(mqttObject);
            //发送的消息
            Map<Character,Integer> has = new LinkedHashMap<>();
            String a = "a";
            System.out.println(0xff);
//            has.put(a.charAt(1),1);
//            has.entrySet().iterator().next().getKey();
            ApiData apiData = new ApiData();
            apiData.setTime(System.currentTimeMillis());
            apiData.setType("SOC");
            apiData.setMsgId("0");
            apiData.setData(encodeBase64("{'sn1':'1' ,'model1':'2','soc1':'3','sn2':'sn2','model2':'2','soc2':'2'}"));
            String jsonStr = JSONObject.toJSONString(apiData);
            gateway.sendMessage2Mqtt(jsonStr,"wootop/r1/wifi");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
